package com.apps.order.service.impl;

import com.apps.globals.activity.enums.ActivityType;
import com.apps.globals.activity.model.po.KupaActivity;
import com.apps.globals.activity.service.IKupaActivityService;
import com.apps.goods.model.po.KupaGoods;
import com.apps.goods.service.IKupaGoodsService;
import com.apps.helper.UserConextHelper;
import com.apps.member.model.po.KupaMemberBuyGoods;
import com.apps.member.service.IKupaMemberBuyGoodsService;
import com.apps.order.enums.OrderStatus;
import com.apps.order.enums.PayType;
import com.apps.order.model.po.KupaGoodsOrder;
import com.apps.order.dao.KupaGoodsOrderMapper;
import com.apps.order.service.IKupaGoodsOrderService;
import com.apps.order.utils.OrderUtils;
import com.apps.sannotation.keys.JoinKey;
import com.apps.saspect.ThreadLock;
import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-10-02
 */
@Service
public class KupaGoodsOrderServiceImpl extends sDaoImpl<KupaGoodsOrderMapper, KupaGoodsOrder> implements IKupaGoodsOrderService
{

    @Autowired
    private IKupaActivityService activityService;

    @Autowired
    private IKupaGoodsService goodsService;

    @Autowired
    private IKupaMemberBuyGoodsService memberBuyGoodsService;
    /**
     * 订单前缀
     */
    private String SnPrefix="goods_";

    @Override
    public KupaGoodsOrder CreateOrder(Integer member_id,Integer goods_id, Integer num)
    {
         KupaGoods goods=goodsService.getOneById(goods_id);
         if(null == goods)
             throw new NullException("商品"+goods_id+"不存在");
         //查找活动
         KupaActivity activity=activityService.getOne(goods.getGoods_cat_id(), ActivityType.GOODS,new BigDecimal(goods.getGoods_price()));
         KupaGoodsOrder goodsOrder=new KupaGoodsOrder();
         goodsOrder.setGoods_id(goods_id);
         goodsOrder.setGoods_price(new BigDecimal(goods.getGoods_price()));
         goodsOrder.setMember_id(member_id);
         goodsOrder.setOrder_status(OrderStatus.WAITPAY.name());
         goodsOrder.setSn(SnPrefix+OrderUtils.TransactionNo());
         goodsOrder.setAmount_price(null != activity.getAmount_price()?activity.getAmount_price():new BigDecimal(0));
         goodsOrder.setPay_price(new BigDecimal(goods.getGoods_price()).subtract(goodsOrder.getAmount_price()));
         return saveToDB(goodsOrder);
    }

    @ThreadLock
    @Override
    public void PaySuccess(String trade_sn, @JoinKey String sn, BigDecimal price, PayType payType)
    {
        if(!sn.contains(SnPrefix))
            return;
        KupaGoodsOrder goodsOrder=getOneByFieldValue("sn",sn);
        if(null == goodsOrder)
            throw new NullException("订单"+sn+"不存在");
        if(!goodsOrder.getOrder_status().equals(OrderStatus.WAITPAY.name()))
            throw new NullException("订单"+sn+"不是待付款状态");
        if(price.compareTo(goodsOrder.getPay_price()) != 0)
            throw new DataException("订单"+sn+"付款金额和订单金额不一致");
        goodsOrder.setOrder_status(OrderStatus.PAYSUCESS.name());
        goodsOrder.setTrade_sn(trade_sn);
        goodsOrder.setPay_type(payType.name());
        updateById(goodsOrder);
        //添加用户购买记录
        KupaMemberBuyGoods buyGoods=new KupaMemberBuyGoods();
        buyGoods.setMember_id(goodsOrder.getMember_id());
        buyGoods.setGoods_id(goodsOrder.getGoods_id());
        buyGoods.setOrder_id(goodsOrder.getId());
        memberBuyGoodsService.saveToDB(buyGoods);
    }
}
